import {
  IsString,
  IsEmail,
  IsOptional,
  IsEnum,
  IsBoolean,
  IsArray,
  IsUUID,
  IsInt,
  IsUrl,
  IsDateString,
  IsJSON,
  IsObject,
  MinLength,
  MaxLength,
  Min,
  Max,
  Matches,
  ValidateNested,
  ArrayMinSize,
  ArrayMaxSize,
  IsPhoneNumber,
  IsNumber
} from 'class-validator'
import { Type, Transform, Expose, Exclude } from 'class-transformer'
import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger'
import { BaseDto, PaginationDto } from './base.dto'

/**
 * 配置类型枚举
 */
export enum ConfigType {
  SYSTEM = 'system',
  SECURITY = 'security',
  EMAIL = 'email',
  SMS = 'sms',
  STORAGE = 'storage',
  CACHE = 'cache',
  PAYMENT = 'payment',
  SOCIAL = 'social',
  SEO = 'seo',
  ANALYTICS = 'analytics',
  NOTIFICATION = 'notification',
  THEME = 'theme',
  CUSTOM = 'custom'
}

/**
 * 配置状态枚举
 */
export enum ConfigStatus {
  ACTIVE = 'active',
  INACTIVE = 'inactive',
  DEPRECATED = 'deprecated',
  TESTING = 'testing'
}

/**
 * 日志级别枚举
 */
export enum LogLevel {
  ERROR = 'error',
  WARN = 'warn',
  INFO = 'info',
  DEBUG = 'debug',
  VERBOSE = 'verbose'
}

/**
 * 存储类型枚举
 */
export enum StorageType {
  LOCAL = 'local',
  AWS_S3 = 'aws_s3',
  ALIYUN_OSS = 'aliyun_oss',
  QINIU = 'qiniu',
  TENCENT_COS = 'tencent_cos',
  MINIO = 'minio'
}

/**
 * 缓存类型枚举
 */
export enum CacheType {
  REDIS = 'redis',
  MEMORY = 'memory',
  MEMCACHED = 'memcached'
}

/**
 * 系统配置DTO
 */
export class SystemConfigDto {
  @ApiProperty({ description: '网站名称' })
  @IsString()
  @MaxLength(100)
  siteName: string

  @ApiProperty({ description: '网站标题' })
  @IsString()
  @MaxLength(200)
  siteTitle: string

  @ApiPropertyOptional({ description: '网站描述' })
  @IsOptional()
  @IsString()
  @MaxLength(500)
  siteDescription?: string

  @ApiPropertyOptional({ description: '网站关键词' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  siteKeywords?: string

  @ApiPropertyOptional({ description: '网站Logo URL' })
  @IsOptional()
  @IsUrl()
  siteLogo?: string

  @ApiPropertyOptional({ description: '网站图标URL' })
  @IsOptional()
  @IsUrl()
  siteFavicon?: string

  @ApiProperty({ description: '网站URL' })
  @IsUrl()
  siteUrl: string

  @ApiPropertyOptional({ description: '管理员邮箱' })
  @IsOptional()
  @IsEmail()
  adminEmail?: string

  @ApiPropertyOptional({ description: '联系邮箱' })
  @IsOptional()
  @IsEmail()
  contactEmail?: string

  @ApiPropertyOptional({ description: '联系电话' })
  @IsOptional()
  @IsPhoneNumber('CN')
  contactPhone?: string

  @ApiPropertyOptional({ description: '公司地址' })
  @IsOptional()
  @IsString()
  @MaxLength(300)
  companyAddress?: string

  @ApiPropertyOptional({ description: '备案号' })
  @IsOptional()
  @IsString()
  @MaxLength(50)
  icp?: string

  @ApiPropertyOptional({ description: '版权信息' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  copyright?: string

  @ApiPropertyOptional({ description: '时区' })
  @IsOptional()
  @IsString()
  @MaxLength(50)
  timezone?: string = 'Asia/Shanghai'

  @ApiPropertyOptional({ description: '默认语言' })
  @IsOptional()
  @IsString()
  @MaxLength(10)
  defaultLanguage?: string = 'zh-CN'

  @ApiPropertyOptional({ description: '是否开启用户注册' })
  @IsOptional()
  @IsBoolean()
  allowRegistration?: boolean = true

  @ApiPropertyOptional({ description: '是否需要邮箱验证' })
  @IsOptional()
  @IsBoolean()
  requireEmailVerification?: boolean = true

  @ApiPropertyOptional({ description: '是否开启维护模式' })
  @IsOptional()
  @IsBoolean()
  maintenanceMode?: boolean = false

  @ApiPropertyOptional({ description: '维护模式提示信息' })
  @IsOptional()
  @IsString()
  @MaxLength(500)
  maintenanceMessage?: string

  @ApiPropertyOptional({ description: '每页默认显示数量' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(100)
  defaultPageSize?: number = 20

  @ApiPropertyOptional({ description: '最大页面大小' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(1000)
  maxPageSize?: number = 100

  @ApiPropertyOptional({ description: '会话超时时间（分钟）' })
  @IsOptional()
  @IsInt()
  @Min(5)
  @Max(10080)
  sessionTimeout?: number = 1440

  @ApiPropertyOptional({ description: '文件上传最大大小（MB）' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(1024)
  maxFileSize?: number = 10

  @ApiPropertyOptional({ description: '允许的文件类型' })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  allowedFileTypes?: string[]

  @ApiPropertyOptional({ description: '是否开启API限流' })
  @IsOptional()
  @IsBoolean()
  enableRateLimit?: boolean = true

  @ApiPropertyOptional({ description: 'API限流次数（每分钟）' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(10000)
  rateLimitCount?: number = 100

  @ApiPropertyOptional({ description: '是否开启HTTPS' })
  @IsOptional()
  @IsBoolean()
  enableHttps?: boolean = false

  @ApiPropertyOptional({ description: '是否开启CDN' })
  @IsOptional()
  @IsBoolean()
  enableCdn?: boolean = false

  @ApiPropertyOptional({ description: 'CDN域名' })
  @IsOptional()
  @IsUrl()
  cdnDomain?: string

  @ApiPropertyOptional({ description: '自定义CSS' })
  @IsOptional()
  @IsString()
  customCss?: string

  @ApiPropertyOptional({ description: '自定义JavaScript' })
  @IsOptional()
  @IsString()
  customJs?: string

  @ApiPropertyOptional({ description: '自定义头部HTML' })
  @IsOptional()
  @IsString()
  customHeadHtml?: string

  @ApiPropertyOptional({ description: '自定义底部HTML' })
  @IsOptional()
  @IsString()
  customFooterHtml?: string
}

/**
 * 安全配置DTO
 */
export class SecurityConfigDto {
  @ApiPropertyOptional({ description: '密码最小长度' })
  @IsOptional()
  @IsInt()
  @Min(6)
  @Max(128)
  passwordMinLength?: number = 8

  @ApiPropertyOptional({ description: '密码是否需要大写字母' })
  @IsOptional()
  @IsBoolean()
  passwordRequireUppercase?: boolean = true

  @ApiPropertyOptional({ description: '密码是否需要小写字母' })
  @IsOptional()
  @IsBoolean()
  passwordRequireLowercase?: boolean = true

  @ApiPropertyOptional({ description: '密码是否需要数字' })
  @IsOptional()
  @IsBoolean()
  passwordRequireNumbers?: boolean = true

  @ApiPropertyOptional({ description: '密码是否需要特殊字符' })
  @IsOptional()
  @IsBoolean()
  passwordRequireSymbols?: boolean = true

  @ApiPropertyOptional({ description: '密码过期天数' })
  @IsOptional()
  @IsInt()
  @Min(0)
  @Max(365)
  passwordExpiryDays?: number = 90

  @ApiPropertyOptional({ description: '密码历史记录数量' })
  @IsOptional()
  @IsInt()
  @Min(0)
  @Max(24)
  passwordHistoryCount?: number = 5

  @ApiPropertyOptional({ description: '登录失败锁定次数' })
  @IsOptional()
  @IsInt()
  @Min(3)
  @Max(20)
  loginFailureLimit?: number = 5

  @ApiPropertyOptional({ description: '账户锁定时间（分钟）' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(1440)
  accountLockoutDuration?: number = 30

  @ApiPropertyOptional({ description: '是否启用双因素认证' })
  @IsOptional()
  @IsBoolean()
  enableTwoFactor?: boolean = false

  @ApiPropertyOptional({ description: '是否强制双因素认证' })
  @IsOptional()
  @IsBoolean()
  forceTwoFactor?: boolean = false

  @ApiPropertyOptional({ description: '是否启用验证码' })
  @IsOptional()
  @IsBoolean()
  enableCaptcha?: boolean = true

  @ApiPropertyOptional({ description: '验证码失败次数阈值' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(10)
  captchaFailureThreshold?: number = 3

  @ApiPropertyOptional({ description: '会话并发限制' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(20)
  sessionConcurrencyLimit?: number = 5

  @ApiPropertyOptional({ description: 'IP白名单' })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  ipWhitelist?: string[]

  @ApiPropertyOptional({ description: 'IP黑名单' })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  ipBlacklist?: string[]

  @ApiPropertyOptional({ description: '是否启用CSRF保护' })
  @IsOptional()
  @IsBoolean()
  enableCsrfProtection?: boolean = true

  @ApiPropertyOptional({ description: '是否启用XSS保护' })
  @IsOptional()
  @IsBoolean()
  enableXssProtection?: boolean = true

  @ApiPropertyOptional({ description: '是否启用内容安全策略' })
  @IsOptional()
  @IsBoolean()
  enableContentSecurityPolicy?: boolean = true

  @ApiPropertyOptional({ description: '内容安全策略配置' })
  @IsOptional()
  @IsString()
  contentSecurityPolicy?: string

  @ApiPropertyOptional({ description: '是否启用HSTS' })
  @IsOptional()
  @IsBoolean()
  enableHsts?: boolean = true

  @ApiPropertyOptional({ description: 'HSTS最大年龄（秒）' })
  @IsOptional()
  @IsInt()
  @Min(0)
  hstsMaxAge?: number = 31536000

  @ApiPropertyOptional({ description: '允许的CORS域名' })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  corsOrigins?: string[]

  @ApiPropertyOptional({ description: '是否记录安全日志' })
  @IsOptional()
  @IsBoolean()
  enableSecurityLogging?: boolean = true

  @ApiPropertyOptional({ description: '安全日志保留天数' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(365)
  securityLogRetentionDays?: number = 90
}

/**
 * 邮件配置DTO
 */
export class EmailConfigDto {
  @ApiProperty({ description: 'SMTP服务器' })
  @IsString()
  @MaxLength(100)
  smtpHost: string

  @ApiProperty({ description: 'SMTP端口' })
  @IsInt()
  @Min(1)
  @Max(65535)
  smtpPort: number

  @ApiPropertyOptional({ description: '是否启用SSL' })
  @IsOptional()
  @IsBoolean()
  smtpSecure?: boolean = false

  @ApiProperty({ description: 'SMTP用户名' })
  @IsString()
  @MaxLength(100)
  smtpUsername: string

  @ApiProperty({ description: 'SMTP密码' })
  @IsString()
  @MaxLength(200)
  smtpPassword: string

  @ApiProperty({ description: '发件人邮箱' })
  @IsEmail()
  fromEmail: string

  @ApiProperty({ description: '发件人名称' })
  @IsString()
  @MaxLength(100)
  fromName: string

  @ApiPropertyOptional({ description: '回复邮箱' })
  @IsOptional()
  @IsEmail()
  replyToEmail?: string

  @ApiPropertyOptional({ description: '邮件模板路径' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  templatePath?: string

  @ApiPropertyOptional({ description: '是否启用邮件队列' })
  @IsOptional()
  @IsBoolean()
  enableQueue?: boolean = true

  @ApiPropertyOptional({ description: '邮件发送限制（每小时）' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(10000)
  sendLimit?: number = 100

  @ApiPropertyOptional({ description: '邮件重试次数' })
  @IsOptional()
  @IsInt()
  @Min(0)
  @Max(10)
  retryAttempts?: number = 3

  @ApiPropertyOptional({ description: '邮件重试延迟（秒）' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(3600)
  retryDelay?: number = 60
}

/**
 * 存储配置DTO
 */
export class StorageConfigDto {
  @ApiProperty({ description: '存储类型', enum: StorageType })
  @IsEnum(StorageType)
  type: StorageType

  @ApiPropertyOptional({ description: '本地存储路径' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  localPath?: string

  @ApiPropertyOptional({ description: 'AWS访问密钥ID' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  awsAccessKeyId?: string

  @ApiPropertyOptional({ description: 'AWS秘密访问密钥' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  awsSecretAccessKey?: string

  @ApiPropertyOptional({ description: 'AWS区域' })
  @IsOptional()
  @IsString()
  @MaxLength(50)
  awsRegion?: string

  @ApiPropertyOptional({ description: 'S3存储桶名称' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  s3Bucket?: string

  @ApiPropertyOptional({ description: 'S3自定义域名' })
  @IsOptional()
  @IsUrl()
  s3CustomDomain?: string

  @ApiPropertyOptional({ description: '阿里云访问密钥ID' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  aliyunAccessKeyId?: string

  @ApiPropertyOptional({ description: '阿里云秘密访问密钥' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  aliyunAccessKeySecret?: string

  @ApiPropertyOptional({ description: '阿里云OSS区域' })
  @IsOptional()
  @IsString()
  @MaxLength(50)
  aliyunRegion?: string

  @ApiPropertyOptional({ description: 'OSS存储桶名称' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  ossBucket?: string

  @ApiPropertyOptional({ description: 'OSS自定义域名' })
  @IsOptional()
  @IsUrl()
  ossCustomDomain?: string

  @ApiPropertyOptional({ description: '七牛访问密钥' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  qiniuAccessKey?: string

  @ApiPropertyOptional({ description: '七牛秘密密钥' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  qiniuSecretKey?: string

  @ApiPropertyOptional({ description: '七牛存储空间' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  qiniuBucket?: string

  @ApiPropertyOptional({ description: '七牛域名' })
  @IsOptional()
  @IsUrl()
  qiniuDomain?: string

  @ApiPropertyOptional({ description: '腾讯云SecretId' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  tencentSecretId?: string

  @ApiPropertyOptional({ description: '腾讯云SecretKey' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  tencentSecretKey?: string

  @ApiPropertyOptional({ description: '腾讯云COS区域' })
  @IsOptional()
  @IsString()
  @MaxLength(50)
  tencentRegion?: string

  @ApiPropertyOptional({ description: 'COS存储桶名称' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  cosBucket?: string

  @ApiPropertyOptional({ description: 'COS自定义域名' })
  @IsOptional()
  @IsUrl()
  cosCustomDomain?: string

  @ApiPropertyOptional({ description: 'MinIO端点' })
  @IsOptional()
  @IsUrl()
  minioEndpoint?: string

  @ApiPropertyOptional({ description: 'MinIO访问密钥' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  minioAccessKey?: string

  @ApiPropertyOptional({ description: 'MinIO秘密密钥' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  minioSecretKey?: string

  @ApiPropertyOptional({ description: 'MinIO存储桶' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  minioBucket?: string

  @ApiPropertyOptional({ description: '是否启用SSL' })
  @IsOptional()
  @IsBoolean()
  minioUseSSL?: boolean = true

  @ApiPropertyOptional({ description: '文件URL前缀' })
  @IsOptional()
  @IsUrl()
  urlPrefix?: string

  @ApiPropertyOptional({ description: '是否启用CDN' })
  @IsOptional()
  @IsBoolean()
  enableCdn?: boolean = false

  @ApiPropertyOptional({ description: 'CDN域名' })
  @IsOptional()
  @IsUrl()
  cdnDomain?: string
}

/**
 * 缓存配置DTO
 */
export class CacheConfigDto {
  @ApiProperty({ description: '缓存类型', enum: CacheType })
  @IsEnum(CacheType)
  type: CacheType

  @ApiPropertyOptional({ description: 'Redis主机' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  redisHost?: string = 'localhost'

  @ApiPropertyOptional({ description: 'Redis端口' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(65535)
  redisPort?: number = 6379

  @ApiPropertyOptional({ description: 'Redis密码' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  redisPassword?: string

  @ApiPropertyOptional({ description: 'Redis数据库索引' })
  @IsOptional()
  @IsInt()
  @Min(0)
  @Max(15)
  redisDatabase?: number = 0

  @ApiPropertyOptional({ description: 'Redis连接超时（毫秒）' })
  @IsOptional()
  @IsInt()
  @Min(1000)
  @Max(60000)
  redisConnectTimeout?: number = 5000

  @ApiPropertyOptional({ description: 'Redis命令超时（毫秒）' })
  @IsOptional()
  @IsInt()
  @Min(1000)
  @Max(60000)
  redisCommandTimeout?: number = 5000

  @ApiPropertyOptional({ description: 'Redis最大重试次数' })
  @IsOptional()
  @IsInt()
  @Min(0)
  @Max(10)
  redisRetryAttempts?: number = 3

  @ApiPropertyOptional({ description: 'Redis重试延迟（毫秒）' })
  @IsOptional()
  @IsInt()
  @Min(100)
  @Max(10000)
  redisRetryDelay?: number = 1000

  @ApiPropertyOptional({ description: '默认缓存TTL（秒）' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(86400)
  defaultTtl?: number = 3600

  @ApiPropertyOptional({ description: '最大缓存大小（MB）' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(1024)
  maxCacheSize?: number = 100

  @ApiPropertyOptional({ description: '是否启用缓存压缩' })
  @IsOptional()
  @IsBoolean()
  enableCompression?: boolean = true

  @ApiPropertyOptional({ description: '缓存键前缀' })
  @IsOptional()
  @IsString()
  @MaxLength(50)
  keyPrefix?: string = 'cms:'
}

/**
 * 配置项DTO
 */
export class ConfigDto extends BaseDto {
  @ApiProperty({ description: '配置键' })
  @IsString()
  @MaxLength(100)
  key: string

  @ApiProperty({ description: '配置值' })
  @IsString()
  value: string

  @ApiProperty({ description: '配置类型', enum: ConfigType })
  @IsEnum(ConfigType)
  type: ConfigType

  @ApiPropertyOptional({ description: '配置描述' })
  @IsOptional()
  @IsString()
  @MaxLength(500)
  description?: string

  @ApiPropertyOptional({ description: '配置分组' })
  @IsOptional()
  @IsString()
  @MaxLength(50)
  group?: string

  @ApiProperty({ description: '配置状态', enum: ConfigStatus })
  @IsEnum(ConfigStatus)
  status: ConfigStatus = ConfigStatus.ACTIVE

  @ApiPropertyOptional({ description: '是否为敏感配置' })
  @IsOptional()
  @IsBoolean()
  sensitive?: boolean = false

  @ApiPropertyOptional({ description: '是否可编辑' })
  @IsOptional()
  @IsBoolean()
  editable?: boolean = true

  @ApiPropertyOptional({ description: '排序权重' })
  @IsOptional()
  @IsInt()
  @Min(0)
  sort?: number = 0

  @ApiPropertyOptional({ description: '配置验证规则' })
  @IsOptional()
  @IsString()
  validationRule?: string

  @ApiPropertyOptional({ description: '默认值' })
  @IsOptional()
  @IsString()
  defaultValue?: string

  @ApiPropertyOptional({ description: '配置选项（JSON格式）' })
  @IsOptional()
  @IsJSON()
  options?: string

  @ApiPropertyOptional({ description: '环境限制' })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  environments?: string[]

  @ApiPropertyOptional({ description: '生效时间' })
  @IsOptional()
  @IsDateString()
  effectiveAt?: string

  @ApiPropertyOptional({ description: '过期时间' })
  @IsOptional()
  @IsDateString()
  expiresAt?: string
}

/**
 * 创建配置DTO
 */
export class CreateConfigDto {
  @ApiProperty({ description: '配置键' })
  @IsString()
  @MaxLength(100)
  key: string

  @ApiProperty({ description: '配置值' })
  @IsString()
  value: string

  @ApiProperty({ description: '配置类型', enum: ConfigType })
  @IsEnum(ConfigType)
  type: ConfigType

  @ApiPropertyOptional({ description: '配置描述' })
  @IsOptional()
  @IsString()
  @MaxLength(500)
  description?: string

  @ApiPropertyOptional({ description: '配置分组' })
  @IsOptional()
  @IsString()
  @MaxLength(50)
  group?: string

  @ApiPropertyOptional({ description: '是否为敏感配置' })
  @IsOptional()
  @IsBoolean()
  sensitive?: boolean = false

  @ApiPropertyOptional({ description: '是否可编辑' })
  @IsOptional()
  @IsBoolean()
  editable?: boolean = true

  @ApiPropertyOptional({ description: '排序权重' })
  @IsOptional()
  @IsInt()
  @Min(0)
  sort?: number = 0

  @ApiPropertyOptional({ description: '配置验证规则' })
  @IsOptional()
  @IsString()
  validationRule?: string

  @ApiPropertyOptional({ description: '默认值' })
  @IsOptional()
  @IsString()
  defaultValue?: string

  @ApiPropertyOptional({ description: '配置选项（JSON格式）' })
  @IsOptional()
  @IsJSON()
  options?: string

  @ApiPropertyOptional({ description: '环境限制' })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  environments?: string[]

  @ApiPropertyOptional({ description: '生效时间' })
  @IsOptional()
  @IsDateString()
  effectiveAt?: string

  @ApiPropertyOptional({ description: '过期时间' })
  @IsOptional()
  @IsDateString()
  expiresAt?: string
}

/**
 * 更新配置DTO
 */
export class UpdateConfigDto extends PartialType(CreateConfigDto) {
  @ApiPropertyOptional({ description: '配置状态', enum: ConfigStatus })
  @IsOptional()
  @IsEnum(ConfigStatus)
  status?: ConfigStatus
}

/**
 * 查询配置DTO
 */
export class QueryConfigDto extends PaginationDto {
  @ApiPropertyOptional({ description: '配置类型', enum: ConfigType })
  @IsOptional()
  @IsEnum(ConfigType)
  type?: ConfigType

  @ApiPropertyOptional({ description: '配置分组' })
  @IsOptional()
  @IsString()
  @MaxLength(50)
  group?: string

  @ApiPropertyOptional({ description: '配置状态', enum: ConfigStatus })
  @IsOptional()
  @IsEnum(ConfigStatus)
  status?: ConfigStatus

  @ApiPropertyOptional({ description: '是否为敏感配置' })
  @IsOptional()
  @Transform(({ value }) => value === 'true')
  @IsBoolean()
  sensitive?: boolean

  @ApiPropertyOptional({ description: '是否可编辑' })
  @IsOptional()
  @Transform(({ value }) => value === 'true')
  @IsBoolean()
  editable?: boolean

  @ApiPropertyOptional({ description: '搜索关键词' })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  search?: string = ''

  @ApiPropertyOptional({ description: '环境' })
  @IsOptional()
  @IsString()
  @MaxLength(20)
  environment?: string
}

/**
 * 批量配置操作DTO
 */
export class BulkConfigOperationDto {
  @ApiProperty({ description: '配置ID列表' })
  @IsArray()
  @IsUUID(4, { each: true })
  @ArrayMinSize(1)
  @ArrayMaxSize(100)
  configIds: string[]

  @ApiProperty({ description: '操作类型' })
  @IsEnum(['activate', 'deactivate', 'delete', 'export'])
  operation: 'activate' | 'deactivate' | 'delete' | 'export'

  @ApiPropertyOptional({ description: '操作原因' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  reason?: string
}

/**
 * 系统信息DTO
 */
export class SystemInfoDto {
  @ApiProperty({ description: '系统版本' })
  @Expose()
  version: string

  @ApiProperty({ description: '构建时间' })
  @Expose()
  buildTime: string

  @ApiProperty({ description: '运行环境' })
  @Expose()
  environment: string

  @ApiProperty({ description: 'Node.js版本' })
  @Expose()
  nodeVersion: string

  @ApiProperty({ description: '操作系统' })
  @Expose()
  platform: string

  @ApiProperty({ description: '系统架构' })
  @Expose()
  architecture: string

  @ApiProperty({ description: '系统运行时间（秒）' })
  @Expose()
  uptime: number

  @ApiProperty({ description: '内存使用情况' })
  @Expose()
  memory: {
    used: number
    total: number
    free: number
    usage: number
  }

  @ApiProperty({ description: 'CPU使用情况' })
  @Expose()
  cpu: {
    model: string
    cores: number
    usage: number
  }

  @ApiProperty({ description: '磁盘使用情况' })
  @Expose()
  disk: {
    used: number
    total: number
    free: number
    usage: number
  }

  @ApiProperty({ description: '数据库连接状态' })
  @Expose()
  database: {
    status: 'connected' | 'disconnected' | 'error'
    responseTime?: number
    error?: string
  }

  @ApiProperty({ description: '缓存连接状态' })
  @Expose()
  cache: {
    status: 'connected' | 'disconnected' | 'error'
    responseTime?: number
    error?: string
  }

  @ApiProperty({ description: '存储状态' })
  @Expose()
  storage: {
    status: 'available' | 'unavailable' | 'error'
    responseTime?: number
    error?: string
  }

  @ApiPropertyOptional({ description: '外部服务状态' })
  @Expose()
  externalServices?: Record<
    string,
    {
      status: 'available' | 'unavailable' | 'error'
      responseTime?: number
      error?: string
    }
  >
}

/**
 * 系统健康检查DTO
 */
export class HealthCheckDto {
  @ApiProperty({ description: '整体健康状态' })
  @Expose()
  status: 'healthy' | 'degraded' | 'unhealthy'

  @ApiProperty({ description: '检查时间' })
  @Expose()
  timestamp: Date

  @ApiProperty({ description: '检查详情' })
  @Expose()
  checks: Record<
    string,
    {
      status: 'pass' | 'fail' | 'warn'
      responseTime?: number
      message?: string
      details?: any
    }
  >

  @ApiProperty({ description: '系统信息' })
  @Expose()
  info: {
    version: string
    uptime: number
    environment: string
  }
}

/**
 * 日志配置DTO
 */
export class LogConfigDto {
  @ApiProperty({ description: '日志级别', enum: LogLevel })
  @IsEnum(LogLevel)
  level: LogLevel = LogLevel.INFO

  @ApiPropertyOptional({ description: '是否启用文件日志' })
  @IsOptional()
  @IsBoolean()
  enableFileLogging?: boolean = true

  @ApiPropertyOptional({ description: '日志文件路径' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  logFilePath?: string = './logs'

  @ApiPropertyOptional({ description: '日志文件最大大小（MB）' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(1024)
  maxFileSize?: number = 20

  @ApiPropertyOptional({ description: '日志文件最大数量' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(100)
  maxFiles?: number = 14

  @ApiPropertyOptional({ description: '日志保留天数' })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(365)
  retentionDays?: number = 30

  @ApiPropertyOptional({ description: '是否启用控制台日志' })
  @IsOptional()
  @IsBoolean()
  enableConsoleLogging?: boolean = true

  @ApiPropertyOptional({ description: '是否启用远程日志' })
  @IsOptional()
  @IsBoolean()
  enableRemoteLogging?: boolean = false

  @ApiPropertyOptional({ description: '远程日志服务器' })
  @IsOptional()
  @IsUrl()
  remoteLogServer?: string

  @ApiPropertyOptional({ description: '远程日志API密钥' })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  remoteLogApiKey?: string

  @ApiPropertyOptional({ description: '是否启用日志压缩' })
  @IsOptional()
  @IsBoolean()
  enableCompression?: boolean = true

  @ApiPropertyOptional({ description: '日志格式' })
  @IsOptional()
  @IsEnum(['json', 'simple', 'combined'])
  format?: string = 'json'

  @ApiPropertyOptional({ description: '是否记录请求日志' })
  @IsOptional()
  @IsBoolean()
  enableRequestLogging?: boolean = true

  @ApiPropertyOptional({ description: '是否记录错误堆栈' })
  @IsOptional()
  @IsBoolean()
  enableStackTrace?: boolean = true

  @ApiPropertyOptional({ description: '敏感字段过滤' })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  sensitiveFields?: string[] = ['password', 'token', 'secret', 'key']
}
